home *** CD-ROM | disk | FTP | other *** search
- Path: goanna.cs.rmit.EDU.AU!not-for-mail
- From: rav@goanna.cs.rmit.EDU.AU (++ robin)
- Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
- Subject: Re: GOTO controversy
- Date: 26 Feb 1996 20:05:18 +1100
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Message-ID: <4grt4e$8fg@goanna.cs.rmit.EDU.AU>
- References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net>
- NNTP-Posting-Host: goanna.cs.rmit.edu.au
- X-Newsreader: NN version 6.5.0 #0 (NOV)
-
- markg@toledo.emi.net (Mark Grosberg) writes:
-
- >Robert C Shields (rcshlds@mailserv.mta.ca) wrote:
- >: I am trying to get a feel for how people feel about the GOTO statement...
- >: please post..
-
- >Somebody at school saw a goto in my code and freaked. Then again, this
- >person (who shall remain nameless) is used to machines with 100MB of
- >memory and dual SPARC CPU's. I on the other hand am not.
-
- >The code in question was a routine that allocated various resources.
- >Below is an example of such a thing (this time using OS/2 & C). I use Goto's
- >to clean up the mess in the event of an error.
-
- >Example for a good use of goto:
- --- ????
- >------------------------------------------------------------------------------
-
- > HEV hev1, hev2, hev3; /* Event semaphores */
- > HMTX hmtx; /* Mutex semaphore */
- > void *ptr;
-
- > if (!DosCreateEventSem(0, &hev1, 0, FALSE))
- > goto hev1_failed;
-
- > if (!DosCreateEventSem(0, &hev2, 0, FALSE))
- > goto hev2_failed;
-
- > if (!DosCreateEventSem(0, &hev3, 0, FALSE))
- > goto hev3_failed;
-
- > if (!DosCreateMutexSem(0, &hmtx, 0, FALSE))
- > goto hmtx_failed;
-
- > if ((ptr = malloc(SOME_SIZE)) == NULL)
- > goto malloc_failed;
-
- > /* Do some stuff here */
- > return TRUE; /* We did okay */
-
- >malloc_failed:
- > DosCloseMutexSem(hmtx);
- >hmtx_failed:
- > DosCloseEventSem(hev3);
- >hev3_failed:
- > DosCloseEventSem(hev2);
- >hev2_failed:
- > DosCloseEventSem(hev1);
- >hev1_failed:
- > return FALSE;
-
- Is there any reason why the GOTOs can't be replaced by
- the particular action routine?
-
- You have the action routines separated from their
- respective IFs, by a bunch of code (which may be large).
- It makes it clumsy to read such code.
-